package ledrpt

import java.text.SimpleDateFormat

class PlasticController {
    def dynamicQueryService

    def Plastic() {
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 10
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime() - 2))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date().clearTime() - 1))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)

        def tableHeadList = []
        def plastic = []
        def pageSizeList = [10, 20, 50, 100]
        def totalCount = 0
        params.SYS_QUERY_NAME = 'plastic'
        plastic = dynamicQueryService.queryForList(params)
        if (plastic.size() > 0) {
            tableHeadList = plastic[0].keySet()
        }
        [pageSize     : pageSize,
         totalCount   : totalCount,
         pageSizeList : pageSizeList,
         tableHeadList: tableHeadList,
         plastic      : plastic,
         startTime    : params.startTime ?: '',
         endTime      : params.endTime ?: '',

        ]
    }

    def exportExcel() {
        def sysQueryName

        sysQueryName = params.SYS_QUERY_NAME

        params.startNo = 1
        params.endNo = 99999999999999999
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList = []
        if (dataList.size() > 0) {
            tableHeadList = dataList[0].keySet()
        }

        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList, tableHeadList: tableHeadList])
    }
}